package com.gxa.wmcrm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.wmcrm.entity.Employee;
import com.gxa.wmcrm.entity.Page;
import com.gxa.wmcrm.service.EmployeeService;
import com.gxa.wmcrm.service.Impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


@WebServlet(name = "UpdateEmployeeServlet", urlPatterns = "/UpdateEmployeeServlet")
public class UpdateEmployeeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");

        //后台接收JSON数据
        InputStream inputStream = request.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        // 字符缓冲区
        char[] buff = new char[1024];
        int length = 0;
        String jsonstr = null;
        while ((length = reader.read(buff)) != -1) {
            // 通过String的构造方法将 字符缓冲区 转化为一个String
            jsonstr = new String(buff, 0, length);
            System.out.println(jsonstr);
        }
        // 通过 FastJSON方法 将JSON结构的字符串转为 map
        Map<String, Object> parameters = JSONObject.parseObject(jsonstr);
        System.out.println("parameters ：" + parameters);
        //创建Page对象并赋值
        int index = (Integer) parameters.get("empId");
        int currentPage = (Integer) parameters.get("currentPage");
        int perSize = (Integer) parameters.get("perSize");
        String empTel = (String) parameters.get("empTel");
        String depotName = (String) parameters.get("depotName");
        String workName = (String) parameters.get("workName");
        Page page = new Page();
        page.setCurrentPage(currentPage);
        page.setPerSize(perSize);
        //调用getEmpId()方法获取EmpId
        EmployeeService employeeService = new EmployeeServiceImpl();
        int empId = employeeService.getEmpId(page, index);
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setEmpTel(empTel);
        employee.setDepotName(depotName);
        employee.setWorkName(workName);
        PrintWriter out = response.getWriter();
        // 准备返回数据 - Map
        Map<String,Object> map = new HashMap<>();
        String message;
        // 调用updateDepartment()方法修改数据
        int action = employeeService.updateEmployee(employee);
        System.out.println(action);
        if (action == 1) {
            // 准备返回数据 - Map
            map.put("Status","success");
            // map转字符串
            message = JSON.toJSONString(map);
            out.print(message);
        } else {
            map.put("Status","error");
            message = JSON.toJSONString(map);
            out.print(message);
        }
    }
}




